﻿using AnQinFrameWorkAPI.DataBase;
using AnQinFrameWorkAPI.Entity.Develop.DTO.DevelopProjectDemand;
using AnQinFrameWorkAPI.Entity.Develop.Entity;
using AnQinFrameWorkAPI.Entity.Develop.View.DevelopProjectDemand;
using AutoMapper;
using AutoMapper.QueryableExtensions;
using FrameWork.DataHelper;
using FrameWork.Entity;
using FrameWork.JWT;
using FrameWork.Request;
using Microsoft.EntityFrameworkCore;
using static AnQinFrameWorkAPI.Common.Enum.DevelopEnum;

namespace AnQinFrameWorkAPI.Service.Develop;

/// <summary>
///     开发管理-项目需求业务层
/// </summary>
public class DevelopProjectDemandService
{
    //数据库
    private readonly DatabaseContext _context;

    //自动映射
    private readonly IMapper _mapper;

    //请求人信息
    private readonly IUserContext _userContext;

    //构造注入
    public DevelopProjectDemandService(IUserContext userContext, DatabaseContext context, IMapper mapper)
    {
        _userContext = userContext;
        _context = context;
        _mapper = mapper;
    }

    #region 获取开发管理项目需求列表

    public async Task<RequestResponseModel> GetProjectDemandList(
        DevelopProjectDemandSearchDTO developProjectDemandSearchDTO)
    {
        //获取筛选
        var query = _context.DevelopProjectDemand
            .ProjectTo<DevelopProjectDemandView>(_mapper.ConfigurationProvider)
            .AsNoTracking().WhereIF(a => a.ProjectId == developProjectDemandSearchDTO.ProjectListId,
                !string.IsNullOrEmpty(developProjectDemandSearchDTO.ProjectListId))
            .WhereIF(a => a.Code.Contains(developProjectDemandSearchDTO.Code),
                !string.IsNullOrEmpty(developProjectDemandSearchDTO.Code))
            .WhereIF(a => a.Name.Contains(developProjectDemandSearchDTO.Name),
                !string.IsNullOrEmpty(developProjectDemandSearchDTO.Name))
            .WhereIF(a => a.CreateTime > developProjectDemandSearchDTO.StartTime,
                developProjectDemandSearchDTO.StartTime != null)
            .WhereIF(a => a.CreateTime < developProjectDemandSearchDTO.EndTime,
                developProjectDemandSearchDTO.EndTime != null)
            .Where(a => a.IsDelete == false);

        //返回列表
        var developProjectDemand = await query
            .OrderBy(a => a.CreateTime)
            .Page(developProjectDemandSearchDTO.PageIndex, developProjectDemandSearchDTO.PageSize)
            .ToListAsync();

        //总数据量
        var pageCount = await query.CountAsync();

        //返回数据
        var pageResult = new PageResult<DevelopProjectDemandView>(developProjectDemand, pageCount);
        return RequestResponse.OK(pageResult, "查询成功");
    }

    #endregion

    #region 新增开发管理项目需求

    public async Task<RequestResponseModel> AddDevelopProjectDemand(
        DevelopProjectDemandAddDTO developProjectDemandAddDTO)
    {
        //校验员工
        var staff = await Valid.ValidId(_context.Staff, _userContext.UserId);
        if (staff == null) return RequestResponse.BadRequest("员工不存在");
        
        //生成新数据
        var developProjectDemand = _mapper.Map<DevelopProjectDemandModel>(developProjectDemandAddDTO);

        developProjectDemand.Id = Guid.NewGuid();
        developProjectDemand.CreateStaffId = staff.Id.ToString();
        developProjectDemand.CreateStaffName = staff.Name;
        developProjectDemand.DevelopItemStatus = DevelopItemStatusEnum.待确认;
        developProjectDemand.ImplementItemStatus = ImplementItemStatusEnum.待测试;
        developProjectDemand.IsDelete = false;
        developProjectDemand.CreateTime = DateTime.Now;

        //新增数据
        _context.DevelopProjectDemand.Add(developProjectDemand);
        await _context.SaveChangesAsync();

        return RequestResponse.OK(developProjectDemand.Id, "新增成功");
    }

    #endregion

    #region 更新开发管理项目需求

    public async Task<RequestResponseModel> PutDevelopProjectDemand(
        DevelopProjectDemandUpdareDTO developProjectDemandUpdare)
    {
        //校验数据
        var query = await Valid.ValidId(_context.DevelopProjectDemand, developProjectDemandUpdare.Id);
        if (query == null) return RequestResponse.ErrorRequest("项目Id无效");

        //编辑数据
        var developProject = _mapper.Map(developProjectDemandUpdare, query);

        //更新表
        _context.DevelopProjectDemand.Update(developProject);
        await _context.SaveChangesAsync();

        return RequestResponse.OK(developProjectDemandUpdare.Id);
    }

    #endregion

    #region 删除开发管理项目需求

    public async Task<RequestResponseModel> DeleteDevelopProjectDemand(SingleIdDTO singleIdDTO)
    {
        //校验数据
        var query = await Valid.ValidId(_context.DevelopProjectDemand, singleIdDTO.Id);
        if (query == null) return RequestResponse.ErrorRequest("项目需求Id无效");
        ;

        query.IsDelete = true;

        //删除操作
        _context.DevelopProjectDemand.Update(query);

        //保存库
        await _context.SaveChangesAsync();
        return RequestResponse.OK(singleIdDTO.Id, "删除成功");
    }

    #endregion

    #region 批量删除开发管理项目需求

    public async Task<RequestResponseModel> DeleteArrayDevelopProjectDemand(IdArrayDTO idArrayDTO)
    {
        //校验单据
        var query = await _context.DevelopProjectDemand
            .WhereIF(a => idArrayDTO.Ids.Contains(a.Id.ToString()), idArrayDTO != null)
            .ToListAsync();

        if (query.Count != idArrayDTO.Ids.Count) return RequestResponse.ErrorRequest("项目需求Id无效");

        foreach (var item in query) item.IsDelete = true;
        ;

        //删除并保存
        _context.DevelopProjectDemand.UpdateRange(query);
        await _context.SaveChangesAsync();
        return RequestResponse.OK(idArrayDTO.Ids, "删除成功");
    }

    #endregion
}